package pl.medec.formmanager.controllers 
{
	import pl.medec.formmanager.FormManager;

	import flash.events.Event;
	import flash.text.TextField;

	/**
	 * @author Piotr Mederak
	 */
	public class TextAreaController extends InputController 
	{
		public function TextAreaController(inputField:TextField, validator:Class, required:Boolean = false, clearDefaultText:Boolean = false, errorInfo:* = null, errorBorderColor:Number = NaN) 
		{
			
			
			var str:String = FormManager.trim( inputField.text );
			
			inputField.text = str;
			
			super( inputField, validator, required, clearDefaultText, errorInfo, errorBorderColor );
			
			
			inputField.addEventListener( Event.CHANGE, onTextInput, false, 0, true );			inputField.addEventListener( Event.SCROLL, onTextScroll, false, 0, true );
		}

		private function onTextScroll(event:Event):void 
		{
			this.textField.scrollV = 0;
			event.stopPropagation( );
		}

		private function onTextInput(event:Event):void 
		{
			
			var maxLines:int = this.textField.height / this.textField.getLineMetrics( 0 ).height;
			
			if(this.textField.height < this.textField.textHeight) 
			{
				var str:String = FormManager.trim( this.textField.text );
				str = str.slice( 0, this.textField.getLineOffset( maxLines ) );
				this.textField.text = str;
				
				this.textField.scrollV = 0;
			} 
			else 
			{
			}
		}
	}
}
